package controllers

import (
	"gitlab.yunzhen-data.com/C-Chain/titan-template/services"
	"gitlab.yunzhen-data.com/C-Chain/titan/bytesutils"
	"gitlab.yunzhen-data.com/C-Chain/titan/httpengine"
	"net/http"
)

type DeployController struct {
	*services.DeployService `inject:""`
}

func NewDeployController() *DeployController {
	return &DeployController{}
}

func (receiver *DeployController) GroupMapping() string {
	return "/apply"
}

func (receiver *DeployController) Mapping() []httpengine.MappingUnit {
	return []httpengine.MappingUnit{
		{
			HTTPMethod:  http.MethodGet,
			PathPattern: "/app",
			Impl:        receiver.applyApp,
			Produce:     nil,
		},
		{
			HTTPMethod:  http.MethodGet,
			PathPattern: "/external",
			Impl:        receiver.applyExternal,
			Produce:     nil,
		},
		{
			HTTPMethod:  http.MethodGet,
			PathPattern: "/endpoints",
			Impl:        receiver.applyEndpoints,
			Produce:     nil,
		},
	}
}

func (receiver *DeployController) Produce() httpengine.ResponseProcessor {
	return new(httpengine.BizJSONResponseProcessor)
}

func (receiver *DeployController) Inject() string {
	return bytesutils.EmptyString
}

/**
 * @api {get} /user/list 查询用户列表
 * @apiVersion 0.1.0
 * @apiGroup User
 * @apiQuery {Number} offset 列表的起始位置
 * @apiQuery {Number} size 查询的数目
 * @apiSuccess {Object[]} users 查询到的用户列表
 * @apiSuccess {Number} users.age 用户年龄
 * @apiSuccess {String} users.name 用户姓名
 */
func (receiver *DeployController) applyApp(request httpengine.Request, response httpengine.Response) (result any, err error) {
	return receiver.DeployService.ApplyApp(
		request.ParamValueWithDefault("config", "0"),
		request.ParamValueWithDefault("app", "0"))
}

/**
 * @api {get} /user/list 查询用户列表
 * @apiVersion 0.1.0
 * @apiGroup User
 * @apiQuery {Number} offset 列表的起始位置
 * @apiQuery {Number} size 查询的数目
 * @apiSuccess {Object[]} users 查询到的用户列表
 * @apiSuccess {Number} users.age 用户年龄
 * @apiSuccess {String} users.name 用户姓名
 */
func (receiver *DeployController) applyExternal(request httpengine.Request, response httpengine.Response) (result any, err error) {
	return receiver.DeployService.ApplyApp(
		request.ParamValueWithDefault("config", "0"),
		request.ParamValueWithDefault("external", "0"))
}

/**
 * @api {get} /user/list 查询用户列表
 * @apiVersion 0.1.0
 * @apiGroup User
 * @apiQuery {Number} offset 列表的起始位置
 * @apiQuery {Number} size 查询的数目
 * @apiSuccess {Object[]} users 查询到的用户列表
 * @apiSuccess {Number} users.age 用户年龄
 * @apiSuccess {String} users.name 用户姓名
 */
func (receiver *DeployController) applyEndpoints(request httpengine.Request, response httpengine.Response) (result any, err error) {
	return receiver.DeployService.ApplyApp(
		request.ParamValueWithDefault("config", "0"),
		request.ParamValueWithDefault("endpoints", "0"))
}
